package jsp;

import util.CookieUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @author 石小俊
 * @date 2023年07月03日 14:46
 */
@WebServlet("*.cookie2")
public class CookieTwoServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        if("/add.cookie2".equals(path)){
            addCookie(request,response);
        }
        if("/findAll.cookie2".equals(path)){
            findAll(request,response);
        }
        if("/modify.cookie2".equals(path)){
            modifyCookie(request,response);
        }
        if("/delete.cookie2".equals(path)){
            deleteCookie(request,response);
        }
    }
    protected void addCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CookieUtil.addCookie("username","admin",response);
        CookieUtil.addCookie("password","123456",response);
        CookieUtil.addCookie("name","管理员",response);
    }

    protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = CookieUtil.findByName("username",request);
        String password = CookieUtil.findByName("password",request);
        String name = CookieUtil.findByName("name",request);
        System.out.println("username:"+username+",password:"+password+",name:"+name);
    }

    protected void modifyCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CookieUtil.modifyCookie("username","alice",response);
    }

    protected void deleteCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CookieUtil.deleteCookie("username",response);
    }


}
